package com.momo.hotel.controller;

import com.momo.hotel.dao.pojo.TbRoomBooktime;
import com.momo.hotel.dao.pojo.TbShopCar;
import com.momo.hotel.dao.pojo.TbUser;
import com.momo.hotel.dao.pojo.ext.TbRoomList;
import com.momo.hotel.dto.SearchHotels;
import com.momo.hotel.service.BookRoomService;
import com.momo.hotel.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class BookRoomController {

    @Autowired
    BookRoomService bookRoomService;

    @Autowired
    ShopCarService shopCarService;

    @RequestMapping(value = "/bookRoom",produces = "application/json;charset=utf-8")
    public void orderRoom(@RequestBody SearchHotels searchHotels, HttpServletRequest request,HttpServletResponse response) throws IOException {

        response.setContentType("text/html;charset=utf-8");
        //获取当前用户
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user!=null){
            //获取要预定的房间ID
            Integer roomId = ((TbRoomList) request.getSession().getAttribute("tbRoomDetail")).getId();
            searchHotels.setRoomId(roomId);

            TbShopCar tbShopCar = new TbShopCar();
            tbShopCar.setUserid(user.getId());
            tbShopCar.setRoomId(roomId);
            tbShopCar.setContact(searchHotels.getPhone());
            tbShopCar.setFlag(1);
            //获取前端传来的入住、离开日期
            String arrivalDate = searchHotels.getArrivalDate();
            String departureDate = searchHotels.getDepartureDate();
            SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
            ParsePosition pos = new ParsePosition(0);
            SimpleDateFormat formatter2 = new SimpleDateFormat("MM/dd/yyyy");
            ParsePosition pos2 = new ParsePosition(0);
            Date arrivalDate1 = formatter.parse(arrivalDate, pos);  //  Mon Jul 01 00:00:00 CST 2019
            Date departureDate1 = formatter2.parse(departureDate, pos2);  //  Thu Jul 18 00:00:00 CST 2019
            tbShopCar.setArrivalDate(arrivalDate1);
            tbShopCar.setDepartureDate(departureDate1);

            boolean book = isBook(searchHotels);
            if(!book){
                    //加入购物车
                    Integer result = shopCarService.addShopCar(tbShopCar);
                    if (result != 0) {
                        response.getWriter().write("加入购物车成功");
                    }else {
                        response.getWriter().write("网络出错,请重新预定！");
                    }
                }else {
                    response.getWriter().write("当前时间已被预定");
                }
        }else {
            response.getWriter().write("请登陆");
        }
    }
    //判断用户选择的时间段是否被预定，true为已被预定
    public boolean isBook(SearchHotels searchHotels){
        List<TbRoomBooktime> tbRoomBooktimeList =bookRoomService.selectAllBook(searchHotels);
        for (TbRoomBooktime item:tbRoomBooktimeList) {
            Date bookArrivalDate = item.getArrivalDate();
            Date bookDepartureDate = item.getDepartureDate();

            String arrivalDate = searchHotels.getArrivalDate();
            String departureDate = searchHotels.getDepartureDate();
            SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
            ParsePosition pos = new ParsePosition(0);
            SimpleDateFormat formatter2 = new SimpleDateFormat("MM/dd/yyyy");
            ParsePosition pos2 = new ParsePosition(0);
            Date arrivalDate1 = formatter.parse(arrivalDate, pos);  //  Mon Jul 01 00:00:00 CST 2019
            Date departureDate1 = formatter2.parse(departureDate, pos2);  //  Thu Jul 18 00:00:00 CST 2019
            //if里面true为已被预定
            if (       (arrivalDate1.getTime() >= bookArrivalDate.getTime() && arrivalDate1.getTime() <= bookDepartureDate.getTime())
                    || (departureDate1.getTime() >= bookArrivalDate.getTime() && departureDate1.getTime() <= bookDepartureDate.getTime())
                    || (bookArrivalDate.getTime() >= arrivalDate1.getTime() && bookArrivalDate.getTime() <= departureDate1.getTime())
                    || (bookDepartureDate.getTime() >= arrivalDate1.getTime() && bookDepartureDate.getTime() <= departureDate1.getTime())
            ){
                return true;
            }
        }
        return false;
    }
}
